package com.java.business.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.java.business.entity.Chapter;
import com.java.business.service.ChapterService;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;


/**
 * 大章
 *
 * @author jl
 * @since 2020-10-05 08:42:54
 */
@RestController
@RequestMapping("admin/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    /**
     * 带条件分页查询大章
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = chapterService.queryPage(params);

	    return R.ok(page);
    }


    /**
     * 根据主键查询大章
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		Chapter chapter = chapterService.getById(id);

        return R.ok(chapter);
    }

    /**
     * 新增/修改
     */
    @PostMapping("/save")
    public R updateOrSave(@RequestBody @Valid Chapter chapter){
        if (StrUtil.isNotBlank(chapter.getId())) {
            chapterService.updateById(chapter);
        } else {
            chapter.setId(UuidUtil.getShortUuid());
            chapterService.save(chapter);
        }

        return R.ok(chapter);
    }

    /**
     * 修改大章
     */
    @PutMapping("/update")
    public R update(@RequestBody Chapter chapter){
		chapterService.updateById(chapter);

        return R.ok();
    }

    /**
     * 删除大章
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
		chapterService.removeById(id);

        return R.ok();
    }

}
